package com.tarcemap.gpsmobileappgui;

import java.util.Iterator;
import java.util.List;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.location.Location;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.tracemap.gpslocation.R;

public class OverlayFriendsLocation extends Overlay {
	List<Location> mLocationList;
	Resources mResources;

	public OverlayFriendsLocation(List<Location> locationList,
			Resources resources) {
		mLocationList = locationList;
		resources = mResources;
	}

	@Override
	public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
			long when) {
		super.draw(canvas, mapView, shadow);
		if (mLocationList.size() == 0)
			return false;

		for (Iterator<Location> iterator = mLocationList.iterator(); iterator
				.hasNext();) {
			Location loc = (Location) iterator.next();
			Paint paint = new Paint();
			Point myScreenCoords = new Point();
			// 将经纬度转换成实际屏幕坐标
			GeoPoint tmpGeoPoint = new GeoPoint(
					(int) (loc.getLatitude() * 1E6),
					(int) (loc.getLongitude() * 1E6));
			mapView.getProjection().toPixels(tmpGeoPoint, myScreenCoords);
			paint.setStrokeWidth(1);
			paint.setARGB(255, 255, 0, 0);
			paint.setStyle(Paint.Style.STROKE);
			Bitmap bmp = BitmapFactory.decodeResource(mResources,
					R.drawable.pushpin);
			canvas.drawBitmap(bmp, myScreenCoords.x, myScreenCoords.y, paint);
			canvas.drawText("Here am I", myScreenCoords.x, myScreenCoords.y,
					paint);
		}

		return true;
	}
}
